#include <glib.h>

#include "gbBook/gbBook.h"
#include "gbBook/gbJournal.h"
#include "gbDlist/gb-dlist.h"
#include "gbUsb/gbIUsb.h"
#include "gbUsb/gbUDisk.h"
#include "glib-object.h"

void dlist_test() {
    // 类的实例化
    GBDList* list;
    // 对象实例化 (构造时初始化属性)
    list = g_object_new(GB_TYPE_DLIST, "head", NULL, "tail", NULL, NULL);

    // 1. 获取属性
    GValue value = G_VALUE_INIT;
    g_value_init(&value, G_TYPE_POINTER);
    g_object_get_property(G_OBJECT(list), "head", &value);
    g_print("0x%X (%s)\n", (guint)(glong)g_value_get_pointer(&value), g_type_name(G_VALUE_TYPE(&value)));
    g_value_unset(&value);

    // 2. 设置属性
    GValue vHead = G_VALUE_INIT, vTail = G_VALUE_INIT;
    g_value_init(&vHead, G_TYPE_POINTER);
    g_value_init(&vTail, G_TYPE_POINTER);
    g_value_set_pointer(&vHead, NULL);  // data pointer
    g_value_set_pointer(&vTail, NULL);
    g_object_set_property(G_OBJECT(list), "head", &vHead);
    g_object_set_property(G_OBJECT(list), "tail", &vTail);
    g_value_unset(&vHead);
    g_value_unset(&vTail);

    // 解除对象引用
    g_object_unref(list);
}

void book_test() {
    GDate* date = g_date_new();
    g_date_set_dmy(date, 8, 2, 2025);

    GBBook* book = g_object_new(
        GB_TYPE_BOOK, "title", "C++ Primer", "author", "Stanley Lippman", "publish_date", date, "price", 323, NULL);

    gb_book_info_print(book);

    GValue val = G_VALUE_INIT;
    g_value_init(&val, G_TYPE_DATE);
    g_object_get_property(G_OBJECT(book), "publish_date", &val);
    GDate* bDate = g_date_new();
    bDate        = g_value_get_boxed(&val);
    g_print(
        "Get GDate Struct Data: %d-%d-%d\n", g_date_get_year(bDate), g_date_get_month(bDate), g_date_get_day(bDate));

    g_value_unset(&val);
    // g_date_free(bDate); // 由于bDate是从val取出,释放val会将bDate一起释放,此处无需再次释放
    g_object_unref(book);
}

void journal_test() {
    GDate* date = g_date_new();
    g_date_set_dmy(date, 1, 7, 2020);

    GBJournal* journal = g_object_new(GB_TYPE_JOURNAL,
                                      "title",
                                      "C++ Primer Plus",
                                      "author",
                                      "Stephen Prata",
                                      "publish_date",
                                      date,
                                      "price",
                                      59,
                                      "desc",
                                      "This is a Journal",
                                      "pages",
                                      678,
                                      NULL);

    gb_journal_info_print(journal);
}

void iusb_test() {
    GBUDisk* udisk = g_object_new(GB_TYPE_UDISK, NULL);
    gb_iusb_write(GB_IUSB(udisk), "Hello UDisk");
    gchar* data = gb_iusb_read(GB_IUSB(udisk));

    g_print("%s\n", data);

    g_print("udisk is IUsb Object? ");
    if (GB_IS_IUSB(udisk))
        g_print("Yes\n");
    else
        g_print("No\n");
    g_print("udisk is UDisk Object? ");
    if (GB_IS_UDISK(udisk))
        g_print("Yes\n");
    else
        g_print("No\n");
}

gint main(gint argc, gchar* argv[]) {

    // dlist_test();
    // book_test();
    // journal_test();
    iusb_test();

    return EXIT_SUCCESS;
}
